package com.lanyuan.a.service.union;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.lanyuan.a.atest.BaseFormMap;
import com.lanyuan.a.bean.Comment;
import com.lanyuan.a.bean.User;
import com.lanyuan.a.bean.Window;
import com.lanyuan.a.bean.WindowSale;
import com.lanyuan.a.dao.BaseDao;
import com.lanyuan.a.service.UserService;
import com.lanyuan.a.service.WindowSaleService;
import com.lanyuan.a.service.WindowService;
import com.lanyuan.plugin.PageView;

@Service("cbsService")
public class CBSService {
	@Resource
	UserService userService;
	@Resource
	private BaseDao<User> baseDao;
	@Resource
	private BaseDao<Window> windowbaseDao;
	@Resource
	private WindowSaleService windowSaleService;
	@Resource
	private WindowService windowService;
	@Resource
	private BaseDao<Comment> commentBaseDao;
	
	public List<Window> windowList()
	{
		return windowService.list();
	}
	public List<User> userList(String root){
		if(root == null) return null;
		return userService.findByRoot(Integer.parseInt(root));
	}
	
	public List<Window> queryWindowsByPage(BaseFormMap formMap) {
		Long count = null;
		String ss = (String)formMap.get("name");
		PageView pageView = (PageView)formMap.get("paging");
		int first = pageView.getPageNow();
		int max = pageView.getPageSize();
		
		
		if(ss != null && !ss.equals("")){
			String hql = "from Window u where c_name like ?";
			String countHql = "select count (1) from Window u where c_name like ? ";
			count = baseDao.count(countHql,new Object[]{"%"+ss+"%"});
			pageView.setRowCount(count);
			return windowbaseDao.find(hql, new Object[]{"%"+ss+"%"}, first, max);
		}else{
			String countHql = "select count (1) from Window";
			count = baseDao.count(countHql,new Object[]{});
			pageView.setRowCount(count);
			return windowbaseDao.find("from Window", new Object[]{}, first , max);
		}
	}
	
	
	public List<User> queryByPage(BaseFormMap formMap , String root) {
		Long count = null;
		String ss = (String)formMap.get("name");
		PageView pageView = (PageView)formMap.get("paging");
		int first = pageView.getPageNow();
		int max = pageView.getPageSize();
		if(ss != null && !ss.equals("")){
			String hql = "from User u where username like ? and root = ?";
			String countHql = "select count (1) from User u where username like ? and root = ?";
			count = baseDao.count(countHql,new Object[]{"%"+ss+"%",Integer.parseInt(root)});
			pageView.setRowCount(count);
			return baseDao.find(hql, new Object[]{"%"+ss+"%",Integer.parseInt(root)}, first, max);
		}else{
			String countHql = "select count (1) from User u where root = ?";
			count = baseDao.count(countHql,new Object[]{Integer.parseInt(root)});
			pageView.setRowCount(count);
			return baseDao.find("from User u where root = ?", new Object[]{Integer.parseInt(root)}, first , max);
		}
	}
	
	public List<User> queryProfitByPage(BaseFormMap formMap , String root) {
		Long count = null;
		String ss = (String)formMap.get("name");
		PageView pageView = (PageView)formMap.get("paging");
		int first = pageView.getPageNow();
		int max = pageView.getPageSize();
		if(ss != null && !ss.equals("")){
			String hql = "from User u where username like ? and root = ?";
			String countHql = "select count (1) from User u where username like ? and root = ?";
			count = baseDao.count(countHql,new Object[]{"%"+ss+"%",Integer.parseInt(root)});
			pageView.setRowCount(count);
			return baseDao.find(hql, new Object[]{"%"+ss+"%",Integer.parseInt(root)}, first, max);
		}else{
			String countHql = "select count (1) from User u where root = ?";
			count = baseDao.count(countHql,new Object[]{Integer.parseInt(root)});
			pageView.setRowCount(count);
			return baseDao.find("from User u where root = ?", new Object[]{Integer.parseInt(root)}, first , max);
		}
	}
	public void test(){
		Date date = new Date();
		List<Window> windowList = windowList();
		for(Window u : windowList){
			List<WindowSale> ws = windowSaleService.queryByWid(u.getId(),date , date);
		}
	}
	
	public List<Comment> queryCommentByPage(BaseFormMap formMap , User user) {
		int  count = 0;
		PageView pageView = (PageView)formMap.get("paging");
		int first = pageView.getPageNow();
		int max = pageView.getPageSize();
		
		List<Window> lw = windowService.list(user.getId());
		
		if(lw.size() <1) return null;
		Integer[] li = new Integer[lw.size()];
		for(int i=0 ; i<lw.size(); i++){
			li[i] = lw.get(i).getW_id();
		}
		String hql = "from Comment cm where cm.w_id in (:w_id) and isdel = '0'";
		count =commentBaseDao.countIn(hql, "w_id", li);
		pageView.setRowCount(count);
		List<Comment> lc = commentBaseDao.selectIn(hql, "w_id", li,first,max);
		
		return lc;
	}
}
